package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqRolePrivilege;
import cloud.maque.admin.domain.request.MenuRoleRequest;
import cloud.maque.admin.domain.request.RoleMenuRequest;
import cloud.maque.admin.mapper.MqRolePrivilegeMapper;
import cloud.maque.admin.service.MqRolePrivilegeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 4color
 * @description mq_role_privilege
 * @date 2023-03-17
 */
@Service
public class MqRolePrivilegeServiceImpl extends ServiceImpl<MqRolePrivilegeMapper, MqRolePrivilege> implements MqRolePrivilegeService {


    @Override
    public Boolean deleteByPrivilegeId(String privilegeId) {

        QueryWrapper<MqRolePrivilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqRolePrivilege::getPrivilegeId, privilegeId);
        return remove(queryWrapper);
    }

    @Override
    public Boolean deleteByRoleId(String roleId) {
        QueryWrapper<MqRolePrivilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqRolePrivilege::getRoleId, roleId);
        return remove(queryWrapper);
    }

    /**
     * 根据角色ID和菜单ID同时存在时，再删除
     *
     * @param roleId
     * @param menuId 这里菜单ID等于权限ID
     * @return
     */
    @Override
    public Boolean deleteByRoleAndMenuId(String roleId, String menuId) {
        QueryWrapper<MqRolePrivilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqRolePrivilege::getRoleId, roleId);
        queryWrapper.lambda().eq(MqRolePrivilege::getPrivilegeId, menuId);
        return remove(queryWrapper);
    }


    /**
     * 给菜单添加角色
     *
     * @param menuRoleRequest
     * @return
     */
    @Override
    @Transactional
    public void addRoleForMenu(MenuRoleRequest menuRoleRequest) {

        //先清空所有关系
        //这里 getMenuId 就是权限 ID
        deleteByPrivilegeId(menuRoleRequest.getMenuId());

        menuRoleRequest.getRoleIds().forEach(item -> {
            MqRolePrivilege privilege = new MqRolePrivilege();
            //这里 getMenuId 就是权限 ID
            privilege.setPrivilegeId(menuRoleRequest.getMenuId());
            privilege.setRoleId(item);
            save(privilege);
        });

    }

    /**
     * 给角色添加菜单
     *
     * @param roleMenuRequest
     * @return
     */
    @Override
    public void addMenuForRole(RoleMenuRequest roleMenuRequest) {

        //先清空所有关系
        //这里 getMenuId 就是权限 ID
        deleteByRoleId(roleMenuRequest.getRoleId());

        roleMenuRequest.getMenuIds().forEach(item -> {
            MqRolePrivilege privilege = new MqRolePrivilege();
            //这里 getMenuId 就是权限 ID
            privilege.setPrivilegeId(item);
            privilege.setRoleId(roleMenuRequest.getRoleId());
            save(privilege);
        });
    }

    /**
     * 单个菜单的权限
     *
     * @param roleId
     * @param menuId
     */
    @Override
    public void addRoleAndMenu(String roleId, String menuId) {
        MqRolePrivilege privilege = new MqRolePrivilege();
        //这里 getMenuId 就是权限 ID
        privilege.setPrivilegeId(menuId);
        privilege.setRoleId(roleId);
        save(privilege);
    }

    @Override
    public List<String> getMenus(String roleID) {

        List<String> arr = new ArrayList<>();
        QueryWrapper<MqRolePrivilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MqRolePrivilege::getRoleId, roleID);
        this.list(queryWrapper).forEach(item -> {
            arr.add(item.getPrivilegeId());
        });
        return arr;
    }


}
